package persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import domain.Game;
import domain.Tournament;

public class TournamentsXMLHandler extends XMLHandler<Tournament> {

	
	
	
	public TournamentsXMLHandler() {

	}
	
	public boolean isTournamentNameUnique(String name) {
		File file = new File(savedDataFolderName+"\\"+name);
		if(file.exists())
			return false;
		else return true;
	}
	
	public String[] getMostRecentTournaments() {
		
		int numberOfTournamentsToShow = 10;
		
		String[] tournaments = new String[numberOfTournamentsToShow];
		File directory = new File(savedDataFolderName);
		File[] files = directory.listFiles();

		Arrays.sort(files, new Comparator<File>(){
		    public int compare(File f1, File f2)
		    {
		        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
		    } });

		for(int teller=0 ; teller<numberOfTournamentsToShow ; teller++)
		{
			tournaments[teller] = files[teller].getName();
		}
		
		return tournaments;
	}

	@Override
	public Tournament readXML(String name, Unmarshaller unMarshaller) {
		Tournament tournament = null;
		try {
			InputStream stream = new FileInputStream(savedDataFolderName+"\\"+name+"\\"+name+".xml");
			tournament = (Tournament) unMarshaller.unmarshal(stream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tournament;
		
		
	}

	

	@Override
	public void writeXML(Tournament tournament,String name, Marshaller m) {
		
		//Create root folder to save all XML files.
		boolean successOne = new File(savedDataFolderName).mkdir();
		//Create subfolder for this specific tournament, folder name = tournament name
		boolean successTwo = new File(savedDataFolderName,tournament.getName()).mkdir();
		
		//DEBUG	  
		if(successOne){
		
			System.out.println("Directory: " + savedDataFolderName + " created" + "XMLHandler.saveTournament");
			  	  
		}
		if(successTwo){
			  System.out.println("Directory: " + tournament.getName() + " created" + "XMLHandler.saveTournament");
		  	  
		}

		
		OutputStream f = null;
		
		try {
			System.out.println(savedDataFolderName+"\\"+tournament.getName()+"\\"+tournament.getName()+".xml");
			f = new FileOutputStream(savedDataFolderName+"\\"+tournament.getName()+"\\"+tournament.getName()+".xml");
			m.marshal(tournament, f);
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				f.close();
			} catch (Exception e) {
			}
		}
		
	}
}